#include "INIReader_extension.hpp"
#include "settingsImport.hpp"
#include "catch2_testDir.hpp"
#include "testFunctions.hpp"

#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_approx.hpp>
#include <filesystem>

using Catch::Approx;
using namespace hms;
namespace fs = std::filesystem;

TEST_CASE("INI reader extension: type deduction"){
	fs::path iniPath { globalVar::testDir };
	iniPath /= "INIReader_extension_test.ini";
	// std::cout << iniPath << "\n";
	ini::INIReader r { getReader(iniPath) };

	SECTION("bool"){
		/* opposite values as in the ini, so that required value MUST come from
		 * the ini */
		bool testVal1 {false}, testVal2 {true};
		{
			using namespace log;
			setLevel(Sink::console, Level::err);
		}
		ini::read(r, "bool", "testVal1", testVal1 );
		ini::read(r, "bool", "testVal2", testVal2 );
		REQUIRE( testVal1 == true  );
		REQUIRE( testVal2 == false );

		/* nonexistent values mustn't be changed by ini::read */
		bool testVal3 {true}, testVal4 {false};
		ini::read(r, "bool", "testVal3", testVal3 );
		ini::read(r, "bool", "testVal4", testVal4 );
		REQUIRE( testVal3 == true  );
		REQUIRE( testVal4 == false );
	}
	SECTION("int"){
		/* testVal1 and 2 exist, 3 mustn't be changed */
		int testVal1 {0}, testVal2 {0}, testVal3{-123};
		ini::read(r, "int", "testVal1", testVal1);
		ini::read(r, "int", "testVal2", testVal2);
		ini::read(r, "int", "testVal3", testVal3);
		REQUIRE( testVal1 == 1 );
		REQUIRE( testVal2 == 123 );
		REQUIRE( testVal3 == -123 );
	}
	SECTION("float"){
		/* testVal1 and 2 exist, 3 mustn't be changed */
		double testVal1 {0}, testVal2 {0}, testVal3{-123};
		ini::read(r, "float", "testVal1", testVal1);
		ini::read(r, "float", "testVal2", testVal2);
		ini::read(r, "float", "testVal3", testVal3);
		REQUIRE( testVal1 == Approx(0.1) );
		REQUIRE( testVal2 == Approx(1.1) );
		REQUIRE( testVal3 == -123 );
	}
}